#pragma once
#ifndef NOIP_2006_4_N2TWO2K_H
#define NOIP_2006_4_N2TWO2K_H

#include <stack>
#include <cmath>
using ll = long long;

ll n2two2k(int n, int k)
{
    ll ret = 0;
    std::stack<int> stk;
    while (n) stk.push(n & 1), n >>= 1;
    while (!stk.empty()) ret += stk.top() * pow(k, stk.size() - 1), stk.pop();
    return ret;
}


#endif